var oclass=$('#tjclass');
var ote=$('#teacher');
var otie=$('#time3');
var otel=$('#tell');
var ors=$('#rens');
var oclass1=$('#tjclass1');
var ote1=$('#teacher1');
var otie1=$('#time2');
var otel1=$('#tell1');
var ors1=$('#rens1');
var obody=$('#tbody1');
var ojianj=$('#zj');
var oPageBox=$('.pagination');
var reg={
    class:/^[\u4E00-\u9FA5]{2}\d{1,2}$/,
    teacher:/^[\u4e00-\u9fa5]{2,6}$/,
    tell:/^1[3-9]\d{9}$/,
    time:/^[1-2]\d{3}\.(0?[1-9]|1[0-2])\.([0-2]\d{1}|3[0-1]|\d{1})$/,
    people:/\d{1,2}/
};
//正则判断
oclass.on('blur',function () {
    var clatex=$(this);
    if (reg.class.test(clatex.val())){
        oclass.removeClass('red');
        oclass.addClass('blue');
        oclass.attr('data-pd','true')
    }else{
        oclass.addClass('red');
        clatex.attr('title','前两位为中文，后面跟一位或者两位数字');
        oclass.attr('data-pd','false')
    }
});
ote.on('blur',function () {
    var teatex=$(this);
    if (reg.teacher.test(teatex.val())){
        ote.removeClass('red');
        ote.addClass('blue');
        ote.attr('data-pd','true')
    }else{
        ote.addClass('red');
        teatex.attr('title','请输入正确的名字');
        ote.attr('data-pd','false')
    }
});
otel.on('blur',function () {
    var teatex=$(this);
    if (reg.tell.test(teatex.val())){
        otel.removeClass('red');
        otel.addClass('blue');
        otel.attr('data-pd','true');
    }else{
        otel.addClass('red');
        teatex.attr('title','请输入正确的手机号');
        otel.attr('data-pd','false')
    }
});
otie.on('blur',function () {
    var clatex=$(this);
    if (reg.time.test(clatex.val())){
        otie.removeClass('red');
        otie.addClass('blue');
        otie.attr('data-pd','true')
    }else{
        otie.addClass('red');
        clatex.attr('title','如2019.01.01');
        otie.attr('data-pd','false')
    }
})
ors.on('blur',function () {
    var clatex=$(this);
    if (reg.people.test(clatex.val())){
        ors.removeClass('red');
        ors.addClass('blue');
        ors.attr('data-pd','true')
    }else{
        ors.addClass('red');
        clatex.attr('title','1为或者2位数');
        ors.attr('data-pd','false')
    }
});
//当满足正则是执行添加
ojianj.click(function () {
    if (oclass.attr('data-pd')=='true' && ote.attr('data-pd')=='true' && otel.attr('data-pd')=='true' && otie.attr('data-pd')=='true' && ors.attr('data-pd')=='true'){
        obody.append(
            ' <tr style="display: none">\n' +
            '            <td class="text-center"><input type="checkbox">'+oclass.val()+'</td>\n' +
            '            <td class="text-center">'+ote.val()+'</td>\n' +
            '            <td class="text-center">'+otel.val()+'</td>\n' +
            '            <td class="text-center">'+otie.val()+'</td>\n' +
            '            <td class="text-center">'+ors.val()+'</td>\n' +
            '            <td class="xiugaibox text-center">\n' +
            '                <button type="button" class="btn btn-primary xiugai" data-toggle="modal" data-target="#myModa2" style="background: #E5C7AB; border: none;"><span class="glyphicon glyphicon-search"></span></button>\n' +
            '                <button data-sanchu type="button" class="btn btn-danger" style="background: #E5C7AB; border:none;"><span class="glyphicon glyphicon-trash"></span></button>\n' +
            '            </td>\n' +
            '        </tr>'
        );
        $('#myModal').attr('tabindex',-1);
        $('button[data-sanchu]').click(function () {
                if (confirm('您确定删除吗？')) {
                    $(this).parent().parent().remove()
                }
            }
        );
        oclass.val('');
        ote.val('');
        otel.val('');
        otie.val('');
        ors.val('');
        getPageNum();
        getPageData(1)
    }
    else{
        //console.log(oclass.attr('data-pd'),ote.attr('data-pd'),otel.attr('data-pd'),otie.attr('data-pd'),ors.attr('data-pd'))
        alert('请输入正确的内容');
        return false
    }
});
//全选
$('#quanxuan').on('click',function () {
        $('#center1 :visible').attr('checked', true)
});
//取消
$('#quxiao').on('click',function () {
    $('#tbody1 :visible').attr('checked', false)
});
//删除
$('button[data-sanchu]').click(function () {
    if (confirm('您确定删除吗？')) {
        $(this).parent().parent().remove()
        getPageNum();
        getPageData(1)
        }
    }
);
//全选删除
$('#quansan').click(function () {
    var n=$('#center1 :checkbox');
    if (confirm('您确定删除吗？')){
        console.log($('#center1 :checkbox[display=block]'))
        for (var i=0;i<n.length;i++){
            if (n[i].checked){
                    n[i].parentNode.parentNode.remove()
           }
        }
    }
    getPageNum();
    getPageData(1)
});
//修改
obody.on('click','.xiugai',function () {
    var apeo=$(this).parent().prev();
    var atime=apeo.prev();
    var atell=atime.prev();
    var atech=atell.prev();
    var aclass=atech.prev();
    oclass1.val(aclass.text());
    ote1.val(atech.text());
    otel1.val(atell.text());
    otie1.val(atime.text());
    ors1.val(apeo.text());
    $('#xiu').on('click',function () {
        $('#xiu').unbind('click');
        apeo.text(ors1.val());
        atime.text(otie1.val());
        atell.text(otel1.val());
        atech.text(ote1.val());
        aclass.html('<input type="checkbox">'+oclass1.val());
    });
});
//创建分页
function getPageNum() {
    var pageNum=Math.ceil(($('#tbody1 tr').length-1)/5);
    oPageBox.html('');
    for(var i=0;i<pageNum;i++){
        var oLi=$('<li>'+'<a href="javascript:;">'+(i+1)+'</a>'+'</li>');
        oPageBox.append(oLi);
        if(i==0){
            oLi.addClass('active');
        }
    }
}
getPageNum();
function getPageData(index) {
    var otr=$('#tbody1 tr');
    otr.hide();
    otr.eq(0).show();
    otr.slice((index-1)*5+1,5*index+1).show()
}
getPageData(1);
oPageBox.on('click','li',function () {
    for(var i=1;i<$('li').length+1;i++){
        if ($(this).children().text()==i){
            getPageData(i)
        }
    }
    $(this).addClass('active').siblings('li').removeClass();
});



